<?php

/**
 *		//内部ajax接口//
 *		//这是一份试验品，由大街编写（zhongmingzi@gmail.com）
 *		//本程序默认环境：Zend Optimizer Yes / 3.3.3
 *		//		Apache/2.0.63 (Win32) PHP/5.2.12
 *		//		MySQL 支持 Yes / client lib version 5.0.89
 *		//		GD library Yes / bundled (2.0.34 compatible)
 *		//		eAccelerator/Memcache/xcache Yes
 *		//		cURL support enabled
 *		//时间：2010 11 28 16:07:55//
 */
header("content-Type: text/html; charset=UTF-8");
define('CURSCRIPT', 'ajax_api');
define('APISCRIPT', true);

require './source/class/class_core.php';
$lab = & core::instance();
$lab->init_setting = true;
$lab->init_user = true;
$lab->init_session = false;
$lab->init_cron = false;
$lab->init_misc = true;
$lab->init_memory = false;
$lab->init();
require libfile('function/favpic');
require libfile('function/post');
require libfile('function/forum');
require libfile('function/member');

$action = getgpc('action', 'P');
//这里进入UPLOAD逻辑,由于在初始类中对上传情况进行了处理，存在auth_uid则认定为上传！
if($_G['auth_uid'] > 0 && !$action) {
	require libfile('class/upload');
	if(!empty($_FILES)) {
		$temp_file = $_FILES['data_'.uploadhash(true)];
		$upid = uploadpic($temp_file, $_G['auth_api_upid']['key']);
		//writelog('upload_pic_error', $_G['auth_api_upid']);
		if($upid > 0) exit('1');
		else {
			$err = array(
					'error_code' => $upid,
					'file' => $temp_file,
					'uid' => $_G['auth_uid'],
				);
			writelog('upload_pic_error', $err);
			exit('-1');
		}
	}
}
$action_array = array('showmyalbumlist', 'addimgtoalbum', 'show_email', 'get_163_info' ,'get_163_album' ,'rebuildface' ,'aidgeturl' ,'getfollow',
						'putusers', 'removeutag', 'adduatag', 'updateaclick', 'favwyalb', 'removefwa', 'removefavimg',
						'addfavimg', 'delimgtag', 'addimgtag', 'edititle', 'deletecomment', 'postcomment','postpost','delthread','addfriend','removefriend',
						'addalbum','removealbum','uploadstring');
$action = !empty($action) && in_array($action, $action_array) ? $action : '';
if(!$action) exit('Access Dennied!');

$key_array = array(
					'action','formhash',
					'doimgfav','idid',//收藏图片按钮
					'ppid','addtagname',//添加或删除图片标签部分
					'title',//修改图片页面标题
					'posttype','typeid','toid','commenttext','comsubmit','type','postid','cid','ispost',//发表评论页面
					'aid',//添加图片到相册
					'fid','tid','authorid',//发表回复帖子页面
					'name','tag',//上传图片的信息
				);
foreach($_POST as $key => $v) {
		$$key = $v;
		if(!in_array($key, $key_array)) unset($$key);
}


if($action == "showmyalbumlist") {//查看我的相册列表
    $alb = showmyalbumlist();
    $al = $alb['data'];
    if(empty($al)) {
        $str1 = "您目前还没有相册，点击<a href=\"".URL."/album/create\" target=\"_blank\">这里</a>建立！";
        exit(chinese($str1, "UTF-8"));
    } else {
        foreach($al as $ak=>$av){
            $str2 .= ">><a class=\"onealb\" id=\"album".$av['aid']."\" href=\"javascript:;\" title='".$av['desc']." ".$av['dateline']."'>".$av['albumname']."</a>";
        }
        exit($str2);
    }
} elseif($action == "addimgtoalbum") {//添加图片到相册
    addimgtoalbum($ppid, $aid);
}elseif($action == "show_email"){
    $show_uc_info = uc_get_user(chinese($req_name, "UTF-8"));
    echo "Email为：".$show_uc_info[2];
}elseif($action == "get_163_info"){
    $userinfo = get_163_list($usern);
    echo "<b>昵称:</b><i>".chinese($userinfo['nickname'],"GBK", "UTF-8")."</i><br /><b>真名:</b><i>".chinese($userinfo['realname'],"GBK", "UTF-8")."</i><br /><b>email:</b><i>".$userinfo['email']."</i><br /><b>手机号码:</b><i>".chinese($userinfo['mobile'],"GBK", "UTF-8")."</i><br /><b>出生年月:</b><i>".$userinfo['birthday']."</i><br /><b>省份:</b><i>".chinese($userinfo['province'],"GBK", "UTF-8")."</i>";
}elseif($action == "get_163_album"){
    $album = get_163_albumlist($usern);
    if($album <= -1) exit("此用户已经被网易删除！");
    else echo "{$usern}的封面已经初始化完毕，您可以关闭此窗口了！";
}elseif($action == "rebuildface"){
    $facedata = rebuildface($url, $aid);
    if($facedata == -1){
        echo "源图片错误！请重建封面讯息！";
    }elseif($facedata == -2){
        echo "生成图片出错！";
    }else{
        $path = wyface_path($aid);
        $faceurl = $path.'/'.$aid.'.jpg';
        update_face_cache(chinese($cachename, 'UTF8', 'GBK'), $cachenamev, $chchetime, $aid, $faceurl);
        echo chinese($facedata, 'GBK', 'UTF8');
    }
}elseif($action == "aidgeturl"){
    $faceurl = aid_get_url($aid);
    if(empty($faceurl)) exit('对不起，此用户已经被网易屏蔽或内部数据错误！');
    $return = "<div class=\"rebuildface\"><a href=\"javascript:;\" onclick=\"javascript:\$.post('ajax_api',{action:'rebuildface',url:'{$faceurl}',aid:'{$aid}',cachename:'{$cachename}',cachenamev:'{$cachenamev}',cachetime:'{$cachetime}'},function(data){\$('#face{$aid}').empty().append(data);});\">恭喜你，资源获取完毕，点击获取封面！</a></div>";
    echo $return;
}elseif($action == "getfollow"){
    if(empty($wytags)) exit("<a color=\"red\" href=\"".URL."/help_search\">没有填写标签！点击返回</a>");
    $follow = getfollow($wyuser);
    if($follow == -1){
        echo "<b>用户名格式错误！</b>";
    }elseif($follow == -2){
        echo "<b>不存在此用户！</b>";
    }elseif(empty($follow)){
        echo "<b>没发现此用户的相似用户，<a href=\"".URL."/help_search\">请返回更换用户</a>！</b>";
    }else{
        $str = "<h2>可以按住CTRL键复选修改</h2><form id=\"selectusers\"><input type=\"hidden\" name=\"action\" value=\"putusers\" /><input type=\"hidden\" name=\"wytags\" value=\"$wytags\" />";
        foreach($follow as $f){
            //$str .= "<li><a href=\"http://photo.163.com/".$f['user']."\" target=\"_blank\">".chinese($f['name'], 'GBK', 'UTF8')."(".$f['user'].")</a></li>";
            $str .= "<p><input style=\"width:20px;\" type=\"checkbox\" name=\"wyid[]\" checked=\"checked\" value=\"".$f['user']." ".$f['id']."\" /><a href=\"http://photo.163.com/".$f['user']."\" target=\"_blank\">".chinese($f['name'], 'GBK', 'UTF8')."(".$f['user'].")</a></p>";
        }//$('#selectusers').serialize(), 
        $str .= "<br /></form><p><input type=\"button\" value=\"提交选中用户\" class=\"subbox\" onclick=\"$.post('ajax_api', $('#selectusers').serialize(),  function(data){ $('#show_aa').html(data);});$('#selectusers').remove();$('.subbox').remove();$('.step4').remove()\" /></p><div id=\"show_aa\"></div>";
        echo $str;
    }
}elseif($action == 'putusers'){
    $ccid = mt_rand(11, 99).substr(time(), 5);
    $tag = chinese($wytags, 'UTF8', 'GBK');
    write_tem($wyid, $ccid);
    addutagrel($wyid, $tag);//关联tag到人
    $userid = do_tem($ccid);//这里返回的是列表里的第一个用户
    echo "<a href=\"".URL."/search?kw={$userid}&action=update_user&isuser=1&ccid={$ccid}\" >恭喜您！数据队列建立完毕，现在将自动跳转到{$userid}的信息重建当中！</a><script type=\"text/JavaScript\">setTimeout(location.href=\"".URL."/search?kw={$userid}&isuser=1&action=update_user&ccid={$ccid}\", 2000);</script>";
}elseif($action == 'removeutag'){
    $tag = chinese($tag, 'UTF8', 'GBK');
    $tagid = removeutag($tag, $user);
    echo $tagid;
    //echo "$(this).parent().hide('fast');";
}elseif($action == 'adduatag'){
    $tag = chinese($tag, 'UTF8', 'GBK');
    $tagid = adduatag($tag, $user);
    echo $tagid;
}elseif($action == 'updateaclick'){
    updateaclick($aid);
}elseif($action == 'favwyalb'){
    favwyalb($aid);
    echo '#'.$aaid;
}elseif($action == 'removefwa'){
    removefwa($aid);
    echo '#'.$aaid;
} elseif($action == 'removefavimg') {//移除图片收藏
    if(removeimgfav($doimgfav)) exit($doimgfav);
    exit;
} elseif($action == 'addfavimg') {// 收藏图片
    if(addimgfav($doimgfav)) exit($doimgfav);
    exit;
} elseif($action == 'removefriend') {//移除粉丝
    if(friend_delete($doimgfav)) exit($doimgfav);
    exit;
} elseif($action == 'addfriend') {// 粉丝某人
	if(!$doimgfav) exit('null');
    if(friend_add($doimgfav)) exit($doimgfav);
    exit;
} elseif($action == 'removealbum') {//移除相册收藏
    if(removefavalbum($doimgfav)) exit($doimgfav);
    exit;
} elseif($action == 'addalbum') {// 收藏相册
    if(addfavalbum($doimgfav)) exit($doimgfav);
    exit;
} elseif($action == 'delimgtag') {//移除图片标签
    if((!empty($addtagname)) && (!empty($ppid))) {
        if($tagid = delimgtag($addtagname, $ppid)) exit($tagid);
        exit;
    }
} elseif($action == 'addimgtag') {//增加图片标签
    if((!empty($addtagname)) && (!empty($ppid))) {
        if($tagid = addimgtag($addtagname, $ppid)) exit($tagid);
        exit('-1');
    }
} elseif($action == 'edititle') {//编辑图片标题
	edititle($ppid, $title);
} elseif($action == 'deletecomment') {//删除评论或回复
	if($ispost == 0)
		$return = delcomment($type, $typeid, $cid, $toid, $postid);
	else 
		$return = delpost($cid, $postid, $type, $typeid, $toid);
    exit("$return");
} elseif($action == 'postcomment') {//发布评论
    $comid = comment($posttype, $typeid, $toid, $commenttext);
    if(isset($comsubmit)) {header('location:'.$_SERVER['HTTP_REFERER']);exit;}
    if($comid) {
		$date2 = time();
		$time = $date2 - TIMESTAMP;
		$return = $comid."|".$time."|".$posttype."|".$typeid."|".$toid;
		//$return = chinese($return, 'GBK', 'UTF8');
		exit($return);
    }
    exit('error|error');
} elseif($action == 'postpost') {//发布回复
    $postid = post($commenttext, $fid, '', $tid);
    if($postid > 0) {
		$date2 = time();
		$time = $date2 - TIMESTAMP;
		$return = $postid."|".$time."|".$fid."|".$tid."|".$authorid;
		//$return = chinese($return, 'GBK', 'UTF8');
		exit($return);
    }
    exit('error|error'); 
} elseif($action == 'delthread') {//删除评论或回复
	$return =  delpost($pid, $postid, $fid, $tid, $authorid, 1);
	if($return == 1) {
		exit($fid);
	}
    exit("$return");
} elseif($action == 'uploadstring') {
	$name = chinese(trim($name), 'UTF-8');
	$tag = chinese(trim($tag), 'UTF-8');
	echo uploadfile(array('name'=>$name, 'tag'=>$tag), true, 20, 1, 1000000, 0, 1);
	exit;
}



?>
